﻿using System;
using System.Management.Automation;
using Nivot.PowerShell.WindowsMobile.Rapi2;

namespace Nivot.PowerShell.WindowsMobile.Commands
{
    [Cmdlet(VerbsLifecycle.Start, "WMProcess")]
    public class StartWMProcessCommand : WMCommandBase
    {
        [Alias("PSPath")]
        [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)]
        public string LiteralPath { get; set; }

        [Parameter(Position = 1, Mandatory = false)]
        public string[] Arguments { get; set; }

        protected override void EndProcessing()
        {
            string devicePath = this.GetUnresolvedProviderPathFromPSPath(LiteralPath);

            string commandLine = null;
            if (Arguments != null)
            {
                commandLine = String.Empty;
                foreach (string arg in Arguments)
                {
                    commandLine += ("\"" + arg + "\" ");
                }
                commandLine.TrimEnd(' ');
            }

            Device.CreateProcess(devicePath, commandLine, ProcessCreationFlags.None);
        }
    }
}